/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.util;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author Le Thanh Giang
 */
public class ValidationUtil {
    public static boolean isValid(String input, String regEx) {
        return isValid(input, Pattern.compile(regEx));
    }

    public static boolean isValid(String input, Pattern regEx) {
        Matcher m = regEx.matcher(input);
        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isUnderMaxLength(String input, int maxLength) {
        if (input.length() < maxLength) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean checkLength(String input, int minLength, int maxLength) {
        if (input.length() < minLength || input.length() > maxLength) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isOverMinLength(String input, int minLength) {
        if (input.length() > minLength) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNumberUnderMaxRange(String input, double max) {
        Pattern p = Pattern.compile(RegularExpression.NUMBER);
        Matcher m = p.matcher(input);
        if (m.find() && (Double.valueOf(input) < max)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNumberOverMinRange(String input, double min) {
        Pattern p = Pattern.compile(RegularExpression.NUMBER);
        Matcher m = p.matcher(input);
        if (m.find() && Double.valueOf(input) > min) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isNumberInRange(String input, double min, double max) {
        Pattern p = Pattern.compile(RegularExpression.NUMBER);
        Matcher m = p.matcher(input);
        if (m.find() && Double.valueOf(input) > min && Double.valueOf(input) < max) {
            return true;
        } else {
            return false;
        }
    }

    public static int dateDiff(Calendar cal) {
        return (cal.compareTo(Calendar.getInstance()));
    }

    public static int dateDiff(Calendar cal1, Calendar cal2) {
        return (cal1.compareTo(cal2));
    }

    public static boolean isEmailValid(String input) {
        boolean isValid = false;
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = input;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    public static boolean isInt(String input) {
        try {
            Integer.parseInt(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isDouble(String input) {
        try {
            Double.parseDouble(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

